package cn.bgs.controller;

import cn.bgs.pojo.Classroom;
import cn.bgs.service.ClassroomService;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.*;

/**
 * @ProjectName: beisheng
 * @Package: cn.bgs.controller
 * @ClassName: ClassroomController
 * @Author: Administrator
 * @Description:
 * @Date: 2021/7/21 0021 21:02
 * @Version: 1.0
 */
@Controller
public class ClassroomController {
    Logger log= LogManager.getLogger();
    @Autowired
    private ClassroomService classroomService;
    @RequestMapping("/classroomJsp")
    public String classroomJsp(){
        return "classroom";
    }

    @RequestMapping("/findClassroom")
    @ResponseBody
    public Map<String,Object> findClassroom(@RequestParam(required=false,defaultValue="1") int page,
                                            @RequestParam(required=false,defaultValue="7") int limit,
                                            String classroomNumber, String classroomName,Integer galleryfulMin,Integer galleryfulMax,String region,String buildingNo,String buildingFloor
                                            ){
        // 使用Pagehelper传入当前页数和页面显示条数会自动为我们的select语句加上limit查询
        // 从他的下一条sql开始分页
        PageHelper.startPage(page, limit);
        List<Classroom> classroom = classroomService.findClassroom(region,buildingNo,buildingFloor,classroomNumber,classroomName,galleryfulMin,galleryfulMax);//这是我们的sql
        // 使用pageInfo包装查询
        PageInfo<Classroom> rolePageInfo = new PageInfo<>(classroom);//
        Map<String,Object> map=new HashMap<String,Object>();
        map.put("code",0);
        map.put("msg","");
        map.put("count",rolePageInfo.getTotal());
        map.put("data",rolePageInfo.getList());
        return map;
    }
    @RequestMapping("/insertSelective")
    @ResponseBody
    public Integer insertSelective(@RequestBody String istDate){

        JSONObject jsonObject=JSONObject.parseObject(istDate);
        Classroom classroom = new Classroom();
        classroom.setRegion(jsonObject.getString("region"));
        classroom.setBuildingNo(jsonObject.getString("buildingNo"));
        classroom.setBuildingFloor(jsonObject.getString("buildingFloor"));
        classroom.setClassroomId(jsonObject.getString("classroomId"));
        classroom.setClassroomNumber(jsonObject.getString("classroomNumber"));
        classroom.setClassroomName(jsonObject.getString("classroomName"));
        classroom.setGalleryful(jsonObject.getInteger("galleryful"));
        classroom.setDeleteFlag(0);
        Calendar now = Calendar.getInstance();//获取时间
        classroom.setCreatedate(now.getTime());
        classroom.setUpdatedate(now.getTime());//当前时间
        return classroomService.insertSelective(classroom);
    }
    @RequestMapping("/deleteByPrimaryKey")
    @ResponseBody
    public Integer deleteByPrimaryKey(@RequestBody String jsonClassroom){

        JSONObject jsonObject =JSONObject.parseObject(jsonClassroom);
        JSONArray ids =jsonObject.getJSONArray("pkIds");
        Integer[] pkIds =new Integer[ids.size()];
        for(int i=0;i<ids.size();i++){
            Object id = ids.get(i);
            pkIds[i]=Integer.parseInt(id.toString());
        }
        List<Integer> list = Arrays.asList(pkIds);
        return classroomService.deleteByPrimaryKey(list);
    }
    @RequestMapping("/classroomDelete")
    @ResponseBody
    public Integer classroomDelete(@RequestBody String data){
        JSONObject jsonObject = JSONObject.parseObject(data);
        Integer pkId=Integer.parseInt(jsonObject.getString("pkId"));
        return classroomService.classroomDelete(pkId);
    }
    @RequestMapping("/updateByPrimaryKeySelective")
    @ResponseBody
    public Integer updateByPrimaryKeySelective(@RequestBody String updData){

        JSONObject jsonObject=JSONObject.parseObject(updData);
        Classroom classroom = new Classroom();
        classroom.setPkId(jsonObject.getInteger("pkId"));
        classroom.setRegion(jsonObject.getString("region"));
        classroom.setBuildingNo(jsonObject.getString("buildingNo"));
        classroom.setBuildingFloor(jsonObject.getString("buildingFloor"));
        classroom.setClassroomId(jsonObject.getString("classroomId"));
        classroom.setClassroomNumber(jsonObject.getString("classroomNumber"));
        classroom.setClassroomName(jsonObject.getString("classroomName"));
        classroom.setGalleryful(jsonObject.getInteger("galleryful"));
        Calendar now = Calendar.getInstance();//获取时间
        classroom.setCreatedate(now.getTime());
        classroom.setUpdatedate(now.getTime());//当前时间
        return classroomService.updateByPrimaryKeySelective(classroom);
    }
    @ResponseBody
    @RequestMapping("/region")
    public List<Classroom> region(String region){
        return classroomService.region(region);
    }
    @ResponseBody
    @RequestMapping("/buildingNo")
    public List<Classroom> buildingNo(String buildingNo){
        return classroomService.buildingNo(buildingNo);
    }
    @ResponseBody
    @RequestMapping("/buildingFloor")
    public List<Classroom> buildingFloor(String buildingFloor){
        return classroomService.buildingFloor(buildingFloor);
    }
}
